from rest_framework.views import set_rollback

from utils.addons.responses import FailedResponse
from utils.mixins.common_mixins.logger import common_logger


# def custom_exception_handler(ex, context):
#     """
#     统一异常拦截处理
#     """
#     set_rollback()
#     common_logger.debug(f'server error: exception={type(ex)}, {ex=}, {context=}')
#
#     return FailedResponse(error=ex, data=getattr(ex, 'data', None))
#
#
# CustomExceptionHandler = custom_exception_handler
# #
class CustomExceptionHandler(FailedResponse):
    def __init__(self, ex, context):
        set_rollback()
        common_logger.debug(f'server error: exception={type(ex)}, {ex=}, {context=}')
        super().__init__(error=ex, data=getattr(ex, 'data', None))